﻿function editUser(username) {
	if (refreshWin('editUser_' + username)) return;
	getUserInfo({
				username : username,
				success : onload
			});

	function onload(user) {
		var txtusername = new Ext.form.TextField({
					fieldLabel : 'Username',
					name : 'username',
					anchor : '100%',
					readOnly : true,
					value : user.username
				});
		var nickname = new Ext.form.TextField({
					fieldLabel : 'Nickname',
					name : 'nickname',
					maxLength : 20,
					anchor : '100%',
					allowBlank : false,
					value : user.nickname
				});
		var email = new Ext.form.TextField({
					fieldLabel : 'Email',
					name : 'email',
					maxLength : 40,
					vtype : 'email',
					anchor : '100%',
					allowBlank : false,
					value : user.email
				});
		var motto = new Ext.form.TextField({
					fieldLabel : 'Motto',
					name : 'motto',
					maxLength : 200,
					anchor : '100%',
					allowBlank : true,
					value : user.motto
				});
		var sid = new Ext.form.TextField({
					fieldLabel : 'SID',
					name : 'sid',
					anchor : '100%',
					readOnly : true,
					value : user.sid
				});
		var name = new Ext.form.TextField({
					fieldLabel : 'Name',
					name : 'name',
					maxLength : 20,
					anchor : '100%',
					allowBlank : true,
					readOnly : !sys.god,//!Ext.isEmpty(user.sid),
					value : user.name
				});
		var school = new Ext.form.TextField({
					fieldLabel : 'School',
					name : 'school',
					maxLength : 100,
					anchor : '100%',
					allowBlank : true,
					readOnly : !sys.god,//!Ext.isEmpty(user.sid),
					value : user.school
				});
		var sex = new Ext.form.ComboBox({
					fieldLabel : 'Sex',
					hiddenName : 'sex',
					mode : 'local',
					triggerAction : 'all',
					valueField : 'value',
					displayField : 'text',
					editable : false,
					allowBlank : false,
					store : new Ext.data.SimpleStore({
								fields : ['value', 'text'],
								data : [['0', '请选择'], ['1', '男'], ['2', '女']]
							}),
					readOnly : !Ext.isEmpty(user.sid),
					value : user.sex || '0'
				});
		var birthday = new Ext.form.DateField({
					fieldLabel : 'Birthday',
					name : 'birthday',
					format : 'Y-m-d',
					anchor : '100%',
					allowBlank : true,
					readOnly : !Ext.isEmpty(user.sid),
					value : string2time(user.birthday)
				});
		var publicinfo = new Ext.form.Checkbox({
					boxLabel : '公开个人信息',
					name : 'publicPersonalInfo',
					checked : user.publicPersonalInfo
				});

		var toolbar = new Ext.Toolbar({
					items : [new Ext.Toolbar.Fill(), new Ext.Button({
										text : '取消',
										iconCls : 'cancel-icon',
										handler : function() {
											win.close();
										}
									}), new Ext.Button({
										text : '保存',
										iconCls : 'ok-icon',
										handler : onUpdate
									})]
				});
		if (sys.user && (username == sys.user.username || sys.god))
			toolbar.insert(0, new Ext.Button({
								text : '更改密码',
								iconCls : 'password-icon',
								handler : onEditPsw
							}));
		if (sys.god) toolbar.insert(1, new Ext.Button({
							text : user.disabled ? '启用用户' : '禁用用户',
							iconCls : 'disable-icon',
							handler : function() {
								setUserDisabled({
											username : user.username,
											disabled : !user.disabled,
											success : function() {
												win.close();
											}
										});
							}
						}));

		var form = new Ext.form.FormPanel({
					frame : true,
					labelWidth : 80,
					items : [new Ext.form.FieldSet({
										title : '基本信息',
										collapsible : true,
										items : [txtusername, nickname, email, motto]
									}), new Ext.form.FieldSet({
										title : '个人信息',
										collapsible : true,
										items : [Ext.isEmpty(user.sid) ? {} : sid, name, school,
												sex, birthday, publicinfo]
									})]
				});

		var win = new Ext.Window({
					id : 'editUser_' + user.username,
					title : '修改资料 - ' + user.username,
					iconCls : 'editinfo-icon',
					width : 400,
					minWidth : 350,
					autoHeight : true,
					shadow : false,
					items : form,
					bbar : toolbar
				});
		win.show();

		function onUpdate() {
			if (!form.getForm().isValid()) {
				alertx('请正确输入数据！');
				return;
			}
			ajaxform({
						form : form,
						url : url.editUserInfo,
						success : function() {
							alertx('资料修改成功！');
							if (username == sys.user.username) refreshMainPage();
							win.close();
						}
					});
		}
		function onEditPsw() {
			editPassword(user.username);
			if (!form.getForm().isDirty()) win.close();
		}
	}
}

function getUserInfo(obj) {
	ajax({
				url : url.getUserInfo,
				params : {
					username : obj.username
				},
				success : obj.success
			});
}
